class UsersController < ApplicationController
    include Account::AccountController
    
    layout 'default'
    before_filter :setup_user, :only =>[:new, :edit, :update, :destroy]
    
    ###################################
    # this method is called after a user successfully signs in
    def logged_in
        redirect_to :action => 'annorations', :login => current_user.login
    end
    
    ###################################
    # Page show if a user is not authorized to manage a resource
    def not_authorized
        render
    end
    
    ###################################
    # GET /users
    # GET /users.xml
    def index
        @page_title = "Users"
        @users = User.find(:all, :select => 'DISTINCT ON (u.login, u.id) u.id, u.login', 
            :joins =>" AS u INNER JOIN annorations ON u.id = annorations.user_id", 
            :order => 'u.login, u.id DESC', 
            :limit => 100)
        
        respond_to do |format|
            format.html # index.rhtml
            format.xml  { render :xml => @users.to_xml }
        end
    end
    
    def welcome
        @page_title = "Welcome"
        render
    end
    
    ###################################
    # GET /users/1
    # GET /users/1.xml
    def show
        @user = User.find_by_login(params[:login])
                
        respond_to do |format|
            format.html # show.rhtml
        end
    end

    ###################################
    # GET /users/1
    # GET /users/1.xml
    def annorations
        @user = User.find_by_login(params[:login])
        @page_title = @user.login + "'s Annorations"
        @authorized_to_edit = true if (current_user && @user.id == current_user.id)
        @annorations = @user.annorations.find(:all, :conditions => 'user_id = ' + @user.id.to_s, :order => 'created_at')
        
        respond_to do |format|
            format.html
            format.xml  {render :action => '../shared/annorations_xml.rxml', :layout=>false }
            format.rss  {render :action => '../shared/annorations_rss.rxml', :layout=>false }
            format.atom {render :action => '../shared/annorations_atom.rxml', :layout=>false }
            format.rdf {render :action => '../shared/annorations_rdf.rxml', :layout=>false }
        end
    end
        
    ###################################
    # GET /users/new
#    def new
#        @user = User.new
#    end
    
    ###################################
    # GET /users/1;edit
    def edit
        render
    end
    
    ###################################
    # PUT /users/1
    # PUT /users/1.xml
    def update
       
        respond_to do |format|
            if @user.update_attributes(params[:user])
                flash[:notice] = 'User was successfully updated.'
                format.html { redirect_to user_url(@user) }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @user.errors.to_xml }
            end
        end
    end
    
    ###################################
    # DELETE /users/1
    # DELETE /users/1.xml
    def destroy
        @user.destroy        
        respond_to do |format|
            format.html { redirect_to users_url }
            format.xml  { head :ok }
        end
    end
    
    protected
    def setup_user
        @user = User.find(params[:id])
        restrict_access(@user, 'admin')
    end
end
